package com.jsfcompref.solutech.backing;


import com.eCommerce.corecontract.dto.CategorieDTO;
import com.eCommerce.corecontract.dto.CommandeDTO;
import com.eCommerce.corecontract.dto.ComposantDTO;
import com.eCommerce.corecontract.exception.EcommerceException;
import com.eCommerce.server.corebackend.util.BackEndConstants;
import com.eCommercecorecontract.svco.IComposantSVCO;
import com.eCommercecorecontract.svco.SVCOFinder;
import com.ecommerceWeb.common.ServiceFinder;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.List;
import java.util.Set;

import javax.faces.component.UIData;
import javax.faces.component.html.HtmlSelectBooleanCheckbox;
import javax.faces.context.FacesContext;

public class ListComposants {
	
	//modif

	 public ListComposants() {
		super();
		// TODO Auto-generated constructor stub
	}
	 
	private UIData data = null;
	private HtmlSelectBooleanCheckbox checkbox;
	
	

	public HtmlSelectBooleanCheckbox getCheckbox() {
		return checkbox;
	}

	public void setCheckbox(HtmlSelectBooleanCheckbox checkbox) {
		this.checkbox = checkbox;
	}

	public UIData getData() {
		return data;
	}

	public void setData(UIData data) {
		this.data = data;
	}

	public List getComposants()
	  {		
			List cmps = null;
			try {
				SVCOFinder sVCOFinder=(SVCOFinder) ServiceFinder.findBean("SVCOFinder");
				IComposantSVCO composantSVCO=(IComposantSVCO)sVCOFinder.get(BackEndConstants.SVCO_COMPOSANT);
				
				cmps = composantSVCO.listerComposants();
			} catch (EcommerceException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}


	  return cmps;
	  }
	
	  public String supprimerSelectedComposants(){
		   try {
			  int size = this.data.getRowCount();
			
			  Set<ComposantDTO> selectedComposants=new HashSet<ComposantDTO>();
			  for(int i=0; i < size; i++){
				  this.data.setRowIndex(i);
				  if(this.checkbox.isSelected()){
					  ComposantDTO checkedComposant = (ComposantDTO) this.getData().getRowData();
					  selectedComposants.add(checkedComposant);
					  //ComposantDTO composant = (ComposantDTO) this.getComposants().get(i);
					 // System.out.println(checkedComposant.getLibelle());
				  }
			  }
			
			SVCOFinder sVCOFinder=(SVCOFinder) ServiceFinder.findBean("SVCOFinder");
			IComposantSVCO composantSVCO=(IComposantSVCO)sVCOFinder.get(BackEndConstants.SVCO_COMPOSANT);
			composantSVCO.supprimerComposants(selectedComposants);
			
		  } catch (EcommerceException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		  
		  return "succes";
	  }
	  

 
}
